<template>
  <el-card class="box-card" style="margin-top: 10px">
    <template #header>
      <div class="clearfix">
        <el-tabs class="tab" v-model="activeName">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visite"></el-tab-pane>
          <el-tab-pane label="订单量" name="order"></el-tab-pane>
        </el-tabs>
        <div class="right-card">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker v-model="date" class="data" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期"></el-date-picker>
        </div>
      </div>
    </template>
    <div>
      <el-row :gutter="20" style="display: flex; flex-wrap: nowrap; align-items: flex-start">
        <div>
          <el-col :span="18">
            <!-- 折线图容器 -->
            <div class="echart" ref="mychart1" id="mychart1" :style="{
              width: '1000px',
              height: '500px',
              border: '1px solid #ccc',
              borderRadius: '20px',
              margin: '20px',
              padding: '20px',
            }"></div>
          </el-col>
        </div>
        <div style="width: 500px; height: '400px'; margin-left: 50px">
          <el-col class="right">
            <h2>门店{{ title }}排名:</h2>
            <ul v-if="title === '销售额'">
              <li v-for="(item, index) in orderRank" :key="index">
                <span class="rindex">{{ item.no }}</span>
                <span class="rvalue">{{ item.name }}：{{ item.money }}元</span>
              </li>
            </ul>
            <ul v-if="title === '访问量'">
              <li v-for="(item, index) in userRank" :key="index">
                <span class="rindex">{{ item.no }}</span>
                <span class="rvalue">{{ item.name }}：{{ item.money }}元</span>
              </li>
            </ul>
            <ul v-if="title === '订单量'">
              <li v-for="(item, index) in seeRank" :key="index">
                <span class="rindex">{{ item.no }}</span>
                <span class="rvalue">{{ item.name }}：{{ item.money }}元</span>
              </li>
            </ul>
          </el-col>
        </div>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import * as echarts from "echarts";
import { mapState, mapGetters } from "vuex";
export default {
  name: "SaleNew",
  data() {
    return {
      activeName: "sale",
      mycharts: null,
      // 收集日历的数据
      date: []
    };
  },
  mounted() {
    this.initEcharts();
    this.$store.dispatch("setHomeList");
  },
  computed: {
    ...mapState({
      homeList: (state) => state.list.homeList,
    }),
    ...mapGetters(["orderRank", "userRank","seeRank"]),
    title() {
      return this.activeName === "sale"
        ? "销售额"
        : this.activeName === "visite"
          ? "访问量"
          : "订单量";
    },
  },

  methods: {
    initEcharts() {
      this.mychart1 = echarts.init(this.$refs.mychart1);
      this.option1 = {
        title: {
          text: this.title,
          left: "center",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["销售额", "访问量", "订单量"],
          left: "left",
          opacity: 0.8, // 图例透明度
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: [],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            name: "销售额",
            type: "bar", // 柱状图
            data: [],
            itemStyle: {
              normal: {
                color: "#009688",
              },
            },
          },
          {
            name: "访问量",
            type: "bar", //
            data: [100, 170, 130, 122, 180, 115, 155, 215, 160, 190, 200, 220],
            itemStyle: {
              normal: {
                  color: "#00BFFF",
              },
            },
          },
          {
            name: "订单量",
            type: "line",
            data: [180, 220, 150, 120, 110, 180, 115, 155, 215, 160, 190, 200],
            itemStyle: {
              normal: {
                lineStyle: {
                  width: 2,
                  color: "#FF9900",
                },
              },
            },
          },
        ],
      };
      this.mychart1.setOption(this.option1);
      window.onresize = () => {
        this.mychart1.resize();
      };
      // mychart1.setOption(option1);
      // window.onresize = () => {
      //   mychart1.resize();
      // };
    },

    setDay() {
      this.date = [
        new Date().toLocaleDateString(),
        new Date().toLocaleDateString(),
      ];
    },
    setWeek() {
      let date = new Date();
      let start = new Date(
        date.getFullYear(),
        date.getMonth(),
        date.getDate() - date.getDay()
      );
      let end = new Date(
        date.getFullYear(),
        date.getMonth(),
        date.getDate() - date.getDay() + 6
      );
      this.date = [start.toLocaleDateString(), end.toLocaleDateString()];
    },
    setMonth() {
      let date = new Date();
      let start = new Date(date.getFullYear(), date.getMonth(), 1);
      let end = new Date(date.getFullYear(), date.getMonth() + 1, 0);
      this.date = [start.toLocaleDateString(), end.toLocaleDateString()];
    },
    setYear() {
      let date = new Date();
      let start = new Date(date.getFullYear(), 0, 1);
      let end = new Date(date.getFullYear() + 1, 0, 0);
      this.date = [start.toLocaleDateString(), end.toLocaleDateString()];
    },
  },

  watch: {
    title() {
      // this.option1.title.text = this.title;
      this.mychart1.setOption({
        title: { text: this.title },
        xAxis: {
          data: this.title === "销售额" ? this.homeList.orderFullYearAxis : this.homeList.userFullYearAxis,
        },
        series: [{
          name: "销售额",
          type: "bar",
          data: this.title === "销售额" ? this.homeList.orderFullYear : this.homeList.userFullYear,
          itemStyle: {
            normal: {
              color: "#009688",
            },
          },
        }]
      });
    },
    homeList() {
      this.option1 = {
        title: {
          text: this.title,
          left: "center",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["销售额", "访问量", "订单量"],
          left: "left",
          opacity: 0.8, // 图例透明度
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.homeList.orderFullYearAxis,
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            name: "销售额",
            type: "bar",
            data: this.homeList.orderFullYear,
            itemStyle: {
              normal: {
                color: "#009688",
              },
            },
          },
          {
            name: "访问量",
            type: "line",
            data: [],
            itemStyle: {
              normal: {
                lineStyle: {
                  width: 2,
                  color: "#00BFFF",
                },
              },
            },
          },
          {
            name: "订单量",
            type: "line",
            data: [180, 220, 150, 120, 110, 180, 115, 155, 215, 160, 190, 200],
            itemStyle: {
              normal: {
                lineStyle: {
                  width: 2,
                  color: "#FF9900",
                },
              },
            },
          },
        ],
      };
      this.mychart1.setOption(this.option1);
      window.onresize = () => {
        this.mychart1.resize();
      };

    }
  },
};
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;

}

.tab {
  width: 62%;
}

.right-card {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-around;
  cursor: pointer;
  font-size: 18px;
}

.right-card span:hover {
  color: #409eff;
  font-weight: bold;
}

.date {
  width: 250px;
  margin: 0px 20px;
}

.right-card span {
  margin: 20px 10px;
}

.charts {
  width: 100%;
  height: 300px;
}

ul {
  list-style: none;
  width: 100%;
  height: 500px;
}

h2 {
  margin: 20px 5px;
}

ul li {
  height: 10%;
  margin: 10px 0px;
}

.rvalue {
  font-size: large;
  margin: 10px;
}

.rindex {
  float: left;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: black;
  color: white;
  text-align: center;
  margin: 5px;
}
</style>